package com.leyou.search.web;


import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 初始化索引库的方法
     * @return
     */
    @GetMapping("/init")
    public ResponseEntity<String> initData() {
        //初始化索引库
        searchService.createIndexAndMapping();
        //导入数据
        searchService.loadData();
        //返回
        return ResponseEntity.ok("索引库初始化OK！");
    }

    /**
     * 自动补全
     * @param key  客户输入的关键字
     * @return      自动补全的字段的集合
     */
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key")String key) {
        //查询,返回
        Mono<List<String>> mono = searchService.getSuggestion(key);
        return mono;
    }

    /**
     * 分页查询商品信息
     * @param searchParamDTO   搜索的请求参数对象
     * @return        商品的分页信息
     */
    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO searchParamDTO) {
        //查询返回
        return searchService.searchGoods(searchParamDTO);
    }

    /**
     * 查询页面顶部的过滤项
     * @param searchParamDTO    搜索的请求参数对象
     * @return                  过滤项的集合
     */
    @PostMapping("/filter")
    public Mono<Map<String, List<?>>> queryFilterList(@RequestBody SearchParamDTO searchParamDTO) {
        //查询返回
        return searchService.queryFilterList(searchParamDTO);
    }
}
